package ${cfg.package_name}.service;

import com.rt.easyjava.common.service.BaseV2ServiceImpl;
<#--import org.springframework.beans.factory.annotation.Autowired;
import java.io.Serializable;
import javax.annotation.Resource;-->
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
<#--import java.lang.reflect.Method;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;-->
import ${cfg.package_name}.dto.${entity}Dto;
import ${cfg.package_name}.model.bean.${entity};
import ${cfg.package_name}.repository.mapper.${entity}Mapper;
<#--import ${cfg.package_base_name}.config.SpringContextHolder;
import com.rt.easyjava.common.base.Result;-->
import lombok.extern.slf4j.Slf4j;

/**
 * ${table.comment!} 服务实现类
 * @author ${author}
 * @since ${date}
 */
@Slf4j
@Service(value = "${table.entityPath}Service")
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class ${entity}Service extends BaseV2ServiceImpl<${entity}Mapper, ${entity}, ${entity}Dto><#-- implements ${entity}BizService-->{

<#--
    @Override
    public Result select4column(String columnName){
        try {
            // vue默认传过来的，出于select option类名考虑，首字母是大写，这样要小字当属性用。
            columnName = columnName.substring(0,1).toLowerCase()+columnName.substring(1);
            Object o = SpringContextHolder.getBean("${table.entityPath}BizMapper");
            Method method = o.getClass().getMethod("mySelect4"+columnName);
            return Result.builder(method.invoke(o));
        }catch (Exception e){
            throw new RuntimeException("方法名不存在");
        }
    }


    // @Resource private MyMapper mapper;
    // @Resource private ${entity}BizMapper ${table.entityPath}BizMapper;

    // ==================================================================================
    // ========== 以下使用的是 Mybatis-plus 实现, 适合单表操作 ==============================
    // ==================================================================================

    /**
    * create a object
    * @param ${table.entityPath}BizDto
    * @return
    */
    @Override
    public Result<${entity}BizDto> create(${entity}BizDto ${table.entityPath}BizDto) {
        return super.create(${table.entityPath}BizDto);
    }

    /**
    * update a object by Id
    * @param ${table.entityPath}BizDto
    * @param updateWrapper
    * @return
    */
    @Override
    public Result<${entity}BizDto> update(${entity}BizDto ${table.entityPath}BizDto, UpdateWrapper updateWrapper) {
        return super.update(${table.entityPath}BizDto, updateWrapper);
    }

    /**
    * 分页 (无合计功能)
    * @param page
    * @param queryWrapper
    * @return
    */
    public Result<${entity}BizDto> page(Page<${entity}Biz> page, QueryWrapper<${entity}Biz> queryWrapper){
        return super.page(page, queryWrapper);
    }

    /**
    * select all by queryWrapper
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<${entity}BizDto> selectBy(QueryWrapper<${entity}Biz> queryWrapper) {
        return super.selectBy(queryWrapper);
    }

    /**
    * select one by id
    * @param id
    * @return
    */
    @Override
    public Result<${entity}BizDto> selectById(Serializable id) {
        return super.selectById(id);
    }


    @Override
    public Result<${entity}BizDto> selectDtoListByKey(String columnName, String key) {
        return super.selectDtoListByKey(columnName, key);
    }



    // ==================================================================================
    // ========== 以下使用的是自定义的 mapper.xml 实现, 适合多表查询 =========================
    // ==================================================================================

    /**
    * 分页查询：mybatis plus 自定义sql分页查询
    * 默认开启合计查询，按需注掉代码
    * @param page
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<${cfg.classNameFirstWordUpper}BizDto> myPage(Page<${table.entityName}Biz> page, QueryWrapper<${table.entityName}Biz> queryWrapper) {
        return super.myPage(page, queryWrapper);
    }

    @Override
    public Result<${cfg.classNameFirstWordUpper}BizDto> myPage(Page<${table.entityName}Biz> page, QueryWrapper<${table.entityName}Biz> queryWrapper, Boolean hasFooter) {
        return super.myPage(page, queryWrapper, hasFooter);
    }

    @Override
    public Result<${cfg.classNameFirstWordUpper}BizDto> mySelectBy(QueryWrapper<${table.entityName}Biz> queryWrapper) {
        return super.mySelectBy(queryWrapper);
    }

    @Override
    public Result<${cfg.classNameFirstWordUpper}BizDto> mySelectById(Serializable id) {
        return super.mySelectById(id);
    }
-->


<#--
    老早以前注掉的，可以删除了
    <#list cfg.columnList as column>
    <#if column.dataResource?exists && column.dataResource==2 && column.optionsSql?exists && column.optionsSql!='' >
    /**
    *  页面上查询列的值，动态加载，sql语句读数据库配置而来
    */
    @Override
    public List mySelect4${column.propertyName}(){
        return super.mySelect4${column.propertyName}();
    }
    </#if>
    </#list>

    /**
    * 返回ID值
    */
    @Override
    public Result<${cfg.classNameFirstWordUpper}Dto> mySave(${cfg.classNameFirstWordUpper}Dto ${cfg.classNameFirstWordLower}Dto){
        Result<${cfg.classNameFirstWordUpper}Dto> result = new Result();

        //封装数据
        ${cfg.classNameFirstWordUpper} ${cfg.classNameFirstWordLower} = mapper.map(${cfg.classNameFirstWordLower}Dto, ${cfg.classNameFirstWordUpper}.class);
        ${cfg.classNameFirstWordLower}.setCreatedate(DateTool.getCurTimestamp());

        //调用mapper
        long i = ${cfg.classNameFirstWordLower}Mapper.myInsert(${cfg.classNameFirstWordLower});
        if(i==0){
            return result.fault("插入失败");
        }

        ${cfg.classNameFirstWordUpper}Dto dto = mapper.map(${cfg.classNameFirstWordLower},${cfg.classNameFirstWordUpper}Dto.class);
        result.setObj(dto);

        return result.success();
    }

    /**
    返回ID值
    */
    @Override
    public Result<${cfg.classNameFirstWordUpper}Dto> myEdit(${cfg.classNameFirstWordUpper}Dto ${cfg.classNameFirstWordLower}Dto){
        Result<${cfg.classNameFirstWordUpper}Dto> result = new Result();

        //封装数据
        ${cfg.classNameFirstWordUpper} ${cfg.classNameFirstWordLower} = mapper.map(${cfg.classNameFirstWordLower}Dto, ${cfg.classNameFirstWordUpper}.class);
        ${cfg.classNameFirstWordLower}.setModifydate(DateTool.getCurTimestamp());

        //调用mapper
        long i = ${cfg.classNameFirstWordLower}Mapper.myUpdateById(${cfg.classNameFirstWordLower});
        if(i==0){
            return result.fault("编辑数据失败");
        }

        ${cfg.classNameFirstWordUpper}Dto dto = mapper.map(${cfg.classNameFirstWordLower},${cfg.classNameFirstWordUpper}Dto.class);
        result.setObj(dto);

        return result.success();
    }

    /**
    伪删除记录，返回ID值
    */
    @Override
    public Result myDeleteById(long id,String adminMan){
        Result result = new Result();

        ${cfg.classNameFirstWordUpper} ${cfg.classNameFirstWordLower} = new ${cfg.classNameFirstWordUpper}();
        ${cfg.classNameFirstWordLower}.setId(id);
        ${cfg.classNameFirstWordLower}.setModifydate(DateTool.getCurTimestamp());
        ${cfg.classNameFirstWordLower}.setModifyman(adminMan);

        //调用mapper
        long i = ${cfg.classNameFirstWordLower}Mapper.myDeleteById(${cfg.classNameFirstWordLower});
        if(i==0){
            return result.fault("删除数据失败");
        }

        return result.success();
    }

-->
}
